<template>
   <div class="app-container">
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets" style="margin-top: 5px"></i>
      <span style="margin-top: 5px">数据列表</span>
      <el-button
        class="btn-add"
        @click="showAddDialog()"
        size="mini">
        添加
      </el-button>
    </el-card>
    <el-dialog>
    </el-dialog>
    <div class="table-container">
      <el-table ref="productCateTable"
                style="width: 100%"
                :data="subjectOptions"
                 border>
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{scope.$index+1}}</template>
        </el-table-column>
        <el-table-column label="学科名称" align="center">
          <template slot-scope="scope">{{scope.row.label}}</template>
        </el-table-column>
       
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="showUpdateDialog(scope.row)">编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog title="添加学科" :visible.sync="addFormVisible" :close-on-click-modal='false'>
	  <el-form :model="addForm" ref="addFormRef" :rules="addFormRule">
	    <el-form-item label="添加学科名称" label-width="`120px`" prop="name">
	      <el-input v-model="addForm.name" autocomplete="off"></el-input>
	    </el-form-item>
	    
	  </el-form>
	  <div slot="footer" class="dialog-footer">
	    <el-button @click="addFormVisible = false">取 消</el-button>
	    <el-button type="primary" @click="handleAdd(addForm.name)">确 定</el-button>
	  </div>
  </el-dialog>
    <el-dialog title="修改学科" :visible.sync="updateFormVisible" :close-on-click-modal='false'>
	  <el-form :model="updateForm" ref="updateFormRef" :rules="updateFormRule">
	    <el-form-item label="修改学科名称" label-width="`120px`" prop="name">
	      <el-input v-model="updateForm.name" autocomplete="off"></el-input>
	    </el-form-item>
	    
	  </el-form>
	  <div slot="footer" class="dialog-footer">
	    <el-button @click="updateFormVisible = false">取 消</el-button>
	    <el-button type="primary" @click="handleUpdate(updateForm.id,updateForm.name)">确 定</el-button>
	  </div>
  </el-dialog>
  </div>
</template>
<script type="text/javascript">
	import {mapGetters} from 'vuex'
	import store from '@/store'
	import {update as updateSubject} from '@/api/subject'
	import {deletes as deleteSubject} from '@/api/subject'
	import {add as addSubject} from '@/api/subject'

	export default {
	    name: "subjectList",
	    data() {
	      return {
	        list: null,
	        //添加学科对话框相应的数据
	        addFormVisible:false,
	        addForm:{
	        	name:null
	        },
	        addFormRule :{
	          name: [{required: true, message: '请输入学科名称', trigger: 'blur'}],
	          
	        },

	        
	        //修改学科对话框相应的数据
	        updateFormVisible:false,
	        updateForm:{
	        	id:null,
	        	name:null
	        },
	        updateFormRule :{
	          name: [{required: true, message: '请输入学科名称', trigger: 'blur'}],
	          
	        },
	        
	       

	      }
	    },
	    computed:{
	    	...mapGetters([
	          'subjectOptions',
	          
	        ]),
	    },
	    methods:{
	    	showAddDialog(){
	    		this.addForm.name=''
	    		this.addFormVisible=true
	    	},
	    	handleAdd(name) {
	    		this.$refs.addFormRef.validate(valid => {
	    			if (valid) {
	    				
			    		addSubject({"name":name}).then(response=>{
			    			
			    			
			    			this.$store.dispatch('GetSubjectList')
				    		this.addFormVisible=false
				    		this.$message({
				                type: 'success',
				                message: '添加成功',
				                duration:1000
				             });


			    		}).catch(error=>{
			    			 this.addFormVisible=false
			    			 this.$message({
				                type: 'error',
				                message: '添加失败',
				                duration:1000
				             });
			    		})
			    	}
        		})
		    },

		    showUpdateDialog(row){
	    		this.updateForm.name=row.label
	    		this.updateForm.id=row.value
	    		this.updateFormVisible=true
	    	},
	    	handleUpdate(id,name) {
        		this.$refs.updateFormRef.validate(valid => {
	    			if (valid) {
		        		updateSubject({"id":id,"name":name}).then(response=>{
		        			
				    		this.$store.dispatch('GetSubjectList')
				    		this.updateFormVisible=false
					    	this.$message({
						                type: 'success',
						                message: '更新成功',
						                duration:1000
						             });
			    			

			    		}).catch(error=>{
			    			this.updateFormVisible=false
					    			 this.$message({
						                type: 'error',
						                message: '更新失败',
						                duration:1000
						             });
			    			})
			    	}
			    })
		    },

		    handleDelete( row) {
		        this.$confirm('是否要删除该学科？如果删除，将彻底删除所有相关的试题，知识点和题目来源！', '提示', {
		          confirmButtonText: '确定',
		          cancelButtonText: '取消',
		          type: 'warning'
		        }).then(() => {
		          	 this.$confirm('再次确认删除该学科？', '提示', {
				          confirmButtonText: '确定',
				          cancelButtonText: '取消',
				          type: 'warning'
				        }).then(() => {
				        	deleteSubject({"id":row.value}).then(response=>{
					    		this.$store.dispatch('GetSubjectList')
					    		this.$message({
						                type: 'success',
						                message: '删除成功',
						                duration:1000
						             });
					    	}).catch(() => {
		          				this.$message({
					                type: 'error',
					                message: '删除失败',
					                duration:1000
					            });
		          
		       				});


				        }).catch(() => {
		          				
		          
		       			 });
		          
		        }).catch(() => {
		          	
		          
		        });
		      }
	    }
	    
	}
	
	
</script>